package com.abucloud.logbackdemo.exception;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author party-abu
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Resp<T> implements Serializable {

    private static Logger LOGGER = LoggerFactory.getLogger(Resp.class);

    public Integer code;

    public String msg;

    public T data;

    public static <T> Resp<T> ok(T data) {

        Resp<T> resp = new Resp<T>();
        resp.setCode(HttpStatus.OK.value());
        resp.setMsg(HttpStatus.OK.getReasonPhrase());
        resp.setData(data);
        return resp;
    }

    public static <T> Resp<T> ok() {

        Resp<T> resp = new Resp<T>();
        resp.setCode(HttpStatus.OK.value());
        resp.setMsg(HttpStatus.OK.getReasonPhrase());
        return resp;
    }

    public static <T> Resp<T> error() {

        Resp<T> resp = new Resp<T>();
        resp.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        resp.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        return resp;
    }

    public static <T> Resp<T> error(String msg) {

        Resp<T> resp = new Resp<T>();
        resp.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        resp.setMsg(msg);
        return resp;
    }

    public static <T> Resp<T> error(Integer code, String msg) {

        Resp<T> resp = new Resp<T>();
        resp.setCode(code);
        resp.setMsg(msg);
        return resp;
    }

    /**
     * 判断请求是否成功
     *
     * @param resp
     * @return
     */
    public static Boolean successful(Resp<Object> resp) {
        return HttpStatus.OK.value() == resp.getCode();
    }

}
